/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sys.donto.telas;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import sys.donto.paciente.Paciente;
import sys.donto.paciente.PacienteDAO;
import sys.donto.paciente.PacienteTableModel;
import sys.donto.prontuario.Prontuario;
import sys.donto.servico.Servico;
import sys.donto.servico.ServicoDAO;
import sys.donto.servico.ServicoTableModel;
import sys.donto.util.Util;

/**
 *
 * @author JOABB
 */
public class telaProntuarioPaciente extends javax.swing.JDialog {
    private Servico servico = new Servico();
    private static Prontuario prontuario;
    private List <Prontuario> listaProntuario;

    public static Prontuario chamaTela(List<Prontuario> prontLista) {

        telaProntuarioPaciente tpront = new telaProntuarioPaciente(prontLista);
        tpront.setVisible(true);
        return prontuario;
    }
    /**
     * Creates new form telaProntuarioPaciente
     */
    
    public telaProntuarioPaciente(List<Prontuario> prontLista) {
        initComponents();
        listaProntuario = prontLista;
        prontuario = new Prontuario();
        setModal(true);
        setLocationRelativeTo(null);
        prontuario.setServi(new ArrayList<Servico>());
        preencheTabela();
    }
    public telaProntuarioPaciente() {
        initComponents();
        setTitle("sysDONTO - Prontuário de Paciente");
    }
    
    private void preencheTabela() {
        ServicoTableModel servTM = new ServicoTableModel(prontuario.getServi());
        tbServico.setModel(servTM);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tfData = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfPaciente = new javax.swing.JTextField();
        tfServico = new javax.swing.JTextField();
        btPesquisaPaciente = new javax.swing.JButton();
        btNovoPaciente = new javax.swing.JButton();
        btPesquisaServico = new javax.swing.JButton();
        btNovoServico = new javax.swing.JButton();
        btAddTabela = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbServico = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Data:");

        tfData.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tfData.setForeground(new java.awt.Color(204, 0, 0));
        tfData.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfData.setEnabled(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Paciente:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Serviço:");

        tfPaciente.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfPaciente.setEnabled(false);
        tfPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfPacienteActionPerformed(evt);
            }
        });

        tfServico.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tfServico.setEnabled(false);

        btPesquisaPaciente.setBackground(new java.awt.Color(0, 153, 153));
        btPesquisaPaciente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/Search 16.png"))); // NOI18N
        btPesquisaPaciente.setToolTipText("Pesquisar Paciente");
        btPesquisaPaciente.setFocusPainted(false);
        btPesquisaPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisaPacienteActionPerformed(evt);
            }
        });

        btNovoPaciente.setBackground(new java.awt.Color(0, 153, 153));
        btNovoPaciente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/add-16.png"))); // NOI18N
        btNovoPaciente.setToolTipText("Novo Paciente");
        btNovoPaciente.setFocusPainted(false);
        btNovoPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoPacienteActionPerformed(evt);
            }
        });

        btPesquisaServico.setBackground(new java.awt.Color(0, 153, 153));
        btPesquisaServico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/Search 16.png"))); // NOI18N
        btPesquisaServico.setToolTipText("Pesquisar Serviço");
        btPesquisaServico.setFocusPainted(false);
        btPesquisaServico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisaServicoActionPerformed(evt);
            }
        });

        btNovoServico.setBackground(new java.awt.Color(0, 153, 153));
        btNovoServico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/add-16.png"))); // NOI18N
        btNovoServico.setToolTipText("Novo Serviço");
        btNovoServico.setFocusPainted(false);
        btNovoServico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoServicoActionPerformed(evt);
            }
        });

        btAddTabela.setBackground(new java.awt.Color(0, 153, 153));
        btAddTabela.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/donto/imagens/6945_16x16.png"))); // NOI18N
        btAddTabela.setToolTipText("Adicionar na Tabela");
        btAddTabela.setFocusPainted(false);
        btAddTabela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddTabelaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfServico))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 457, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfData, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btPesquisaPaciente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btNovoPaciente))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btPesquisaServico)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btNovoServico)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btAddTabela)))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisaPaciente)
                    .addComponent(btNovoPaciente))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfServico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisaServico)
                    .addComponent(btNovoServico)
                    .addComponent(btAddTabela))
                .addContainerGap(255, Short.MAX_VALUE))
        );

        tbServico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbServico);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 574, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(110, 110, 110)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(95, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void limpaCampos(){
        tfData.setText("");
        tfPaciente.setText("");
        tfServico.setText("");
    }
    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        Date datasistema = new Date(); //a data do sistema é no tipo Date
        SimpleDateFormat formato = new SimpleDateFormat ("dd/MM/yyyy"); //instanciei um formato de data do sistema e alterei para uma label
        tfData.setText(formato.format(datasistema));
    }//GEN-LAST:event_formWindowOpened

    private void tfPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfPacienteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfPacienteActionPerformed

    private void btAddTabelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddTabelaActionPerformed
         if (!Util.chkVazio(tfData.getText(),tfPaciente.getText(),tfServico.getText())) {
                         
                prontuario.getServi().add(servico);
                servico = new Servico();
                tfServico.setText("");
            
        preencheTabela();
      
         }
        

    }//GEN-LAST:event_btAddTabelaActionPerformed

    private void btPesquisaPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisaPacienteActionPerformed
        PacienteDAO dao = new PacienteDAO();// pesquisa de Paciente.
        List<Paciente> lista = dao.listar();
        PacienteTableModel ptm = new PacienteTableModel(lista);
        Object o = telaPesquisa.exibeTela(ptm, "paciente");
        Paciente p;
        if (o != null) {
            p = dao.pesquisaId(Integer.valueOf(String.valueOf(o)));
            prontuario.setPaciente(p);
            tfPaciente.setText(p.getNome());
        }
    }//GEN-LAST:event_btPesquisaPacienteActionPerformed

    private void btPesquisaServicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisaServicoActionPerformed
        ServicoDAO dao = new ServicoDAO();// pesquisa de Servico.
        List<Servico> lista = dao.listar();
        ServicoTableModel stm = new ServicoTableModel(lista);
        Object o = telaPesquisa.exibeTela(stm, "servico");
        Servico s;
        if (o != null) {
            s = dao.pesquisaId(Integer.valueOf(String.valueOf(o)));
            prontuario.setServi(s);
            tfServico.setText(s.getDescricao());
        }
    }//GEN-LAST:event_btPesquisaServicoActionPerformed

    private void btNovoPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoPacienteActionPerformed
        telaPaciente tpac = new telaPaciente();
        tpac.setVisible(true);
    }//GEN-LAST:event_btNovoPacienteActionPerformed

    private void btNovoServicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoServicoActionPerformed
        telaServico tserv = new telaServico();
        tserv.setVisible(true);
    }//GEN-LAST:event_btNovoServicoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(telaProntuarioPaciente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(telaProntuarioPaciente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(telaProntuarioPaciente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(telaProntuarioPaciente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new telaProntuarioPaciente().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddTabela;
    private javax.swing.JButton btNovoPaciente;
    private javax.swing.JButton btNovoServico;
    private javax.swing.JButton btPesquisaPaciente;
    private javax.swing.JButton btPesquisaServico;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbServico;
    private javax.swing.JTextField tfData;
    private javax.swing.JTextField tfPaciente;
    private javax.swing.JTextField tfServico;
    // End of variables declaration//GEN-END:variables
}
